﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using HallTimer.Classes;
using HallTimer.Interfaces;
using HallTimer.Views.Subwindows;
using HallTimer.Views.Outputpages;

namespace HallTimer.Views
{
    /// <summary>
    /// Logic of Main Window: sends the right messages to the inner pages, and other global control
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        public static MainWindow mainWindowRef = null;
        public static SecondOutputWindow secondWindowRef = null;
        //private CountDownTimer timer;
        private Page currentPage;

        public MainWindow()
        {
            InitializeComponent();

            mainWindowRef = this;
            // onLoadCompleted => executed when new embedded Page is loaded; that way, we can have a reference to the Page (and send 
            mainFrame.NavigationService.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(NavigationService_LoadCompleted);
            secondWindowRef = new SecondOutputWindow();
            secondWindowRef.Show();
        }

        private void closeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        public void NavigationService_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            currentPage = ((Page)e.Content);
        }

        /// <summary>
        /// Generic event handler for all buttons in the Timer ribbon tab
        /// </summary>
        public void onTimerButton_Click(object sender, RoutedEventArgs e)
        {
            if ((currentPage is TimerPage) && (sender is Button))
            {
                TimerPage timerpage = (TimerPage)this.currentPage;
                Button button = (Button)sender;
                switch (button.Name)
                {
                    case "timerStartButton":
                        secondWindowRef.ChangeOutputWindow(SecondOutputWindow.Screentype.Timing);
                        timerpage.startTimer();
                        timerStartButton.IsEnabled = false;
                        timerStopButton.IsEnabled = true;
                        liedTab.IsEnabled = false;
                        //timer = new CountDownTimer(this, 5);
                        break;
                    case "timerStopButton":
                        StoppingTimer(timerpage);
                        break;
                    // no default case
                }
            }
        }

        public void StoppingTimer(TimerPage timerpage)
        {
            secondWindowRef.ChangeOutputWindow(SecondOutputWindow.Screentype.Empty);
            timerpage.stopTimer();
            timerStopButton.IsEnabled = false;
            liedTab.IsEnabled = true;
        }

        private void onSongButton_Click(object sender, RoutedEventArgs e)
        {
            SongPage songpage = (SongPage)this.currentPage;
            String currentLiednummer = songpage.GetCurrentSelectedSong();
            Page currentOutputPage = secondWindowRef.CurrentOutputPage;
            if (currentOutputPage.GetType().Equals(typeof(SongOutputPage)))
            {
                SongOutputPage outputPage = (SongOutputPage)secondWindowRef.CurrentOutputPage;
                outputPage.SetSongNumber(currentLiednummer);
            }       
        }

        /// <summary>
        /// SelectionChanged event - also fired when starting the application
        /// </summary>
        private void onTab_Click(object sender, SelectionChangedEventArgs e)
        {
            Ribbon ribbon = (Ribbon)sender;
            RibbonTab selectedTab = (RibbonTab)ribbon.SelectedItem;
            String pageURI = String.Empty;
            switch (selectedTab.Name)
            {
                case "timerTab":
                    pageURI = "Subpages/TimerPage.xaml";
                    secondWindowRef.ChangeOutputWindow(SecondOutputWindow.Screentype.Empty);
                    break;
                case "liedTab":
                    pageURI = "Subpages/SongPage.xaml";
                    secondWindowRef.ChangeOutputWindow(SecondOutputWindow.Screentype.Song);
                    break;
                // no default case possible
            }
            if (pageURI != String.Empty) {
                mainFrame.Source = new Uri(pageURI, UriKind.Relative);
            }
        }

        public void ActivateTimerStartButton()
        {
            timerStartButton.IsEnabled = true;
        }

        public void SetTimerValue(int minutesLeft, int secondsLeft)
        {
            Page currentOutputPage = secondWindowRef.CurrentOutputPage;
            if (currentOutputPage.GetType().Equals(typeof(TimerOutputPage)) )
            {
                TimerOutputPage outputPage = (TimerOutputPage)secondWindowRef.CurrentOutputPage;
                outputPage.ChangeTimeValue(minutesLeft, secondsLeft);
            }            

            // TODO: send values to status bar too
        }

        private void RibbonWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            secondWindowRef.Close();
        }


    }
}
